//
//  XMSignView.m
//  XunMi
//
//  Created by MacBook on 2024/4/15.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMSignView.h"
#import "XMSignCollectionViewCell.h"
#import "XMSignModel.h"
#import "XMSignRuleView.h"

@interface XMSignView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *signDayLa;
@property (weak, nonatomic) IBOutlet UILabel *signDescLa;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) XMSignRuleView *ruleView;
@end

@implementation XMSignView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubviews];
}

- (void)setModel:(XMSignModel *)model {
    _model = model;
    self.signDayLa.text = kStringFormat(@"已连续签到%@天", model.count);
    
    self.signDescLa.text = model.rules;
    
    self.ruleView.ruleLa.text = model.describe;
    self.ruleView.titleLa.text = model.title;

    [self.collectionView reloadData];
}

#pragma mark - setupSubviews

- (void)setupSubviews {
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.itemSize = CGSizeMake(255 / 6, 50);
    fl.minimumLineSpacing = 10;
    fl.minimumInteritemSpacing = 10;
    
    [self.collectionView setCollectionViewLayout:fl];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMSignCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMSignCollectionViewCell)];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.table.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMSignCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMSignCollectionViewCell) forIndexPath:indexPath];
    cell.model = [self.model.table safe_objectAtIndex:indexPath.item];
    return cell;
}

- (IBAction)desc:(UIButton *)sender {
    [self addSubview:self.ruleView];
}

- (IBAction)sign:(UIButton *)sender {
    if (self.signBlock) {
        self.signBlock(self.model.index);
    }
}

- (IBAction)close:(id)sender {
    if (self.closeBlock) {
        self.closeBlock();
    }
}


- (XMSignRuleView *)ruleView {
    if (_ruleView == nil) {
        _ruleView = [XMSignRuleView viewFromXib];
        _ruleView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _ruleView;
}

@end
